//
//  HompageViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/24.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class HompageViewController: UIViewController,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate {
    
    var modelArray:NSMutableArray? = NSMutableArray()
    var model:DataModel?
    var homeTable = UITableView()
    var scrollView = UIScrollView()
    var imagesURLStrings = NSMutableArray()
    var imageH:CGFloat = 250.0
    var cycleScrollView:SDCycleScrollView?
    var cycleScrollView2:SDCycleScrollView?
    var notice = NSMutableArray()
    var titleDemo:LLCycleScrollView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.black
    
        
//        页面滑动
        scrollView.frame = CGRect(x: 0, y: 0, width: w, height: h)
        scrollView.contentSize = CGSize(width: w, height: h )
        scrollView.backgroundColor = UIColor.black
        scrollView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -10)
        scrollView.delegate = self
        self.view.addSubview(scrollView)

//        轮播图
        cycleScrollView = SDCycleScrollView.init(frame: CGRect(x: 0, y: 0, width: w, height: 150))
        cycleScrollView?.delegate = self
        cycleScrollView?.pageControlAliment = SDCycleScrollViewPageContolAlimentRight
        cycleScrollView?.autoScrollTimeInterval = 5
//        cycleScrollView.titlesGroup = title as! [Any]   //轮播图标题
        cycleScrollView?.currentPageDotColor = UIColor.black
        self.scrollView.addSubview(cycleScrollView!)
        
//        通知
//        cycleScrollView2 = SDCycleScrollView.init(frame: CGRect(x: 0, y: 150, width: w, height: 120))
//        cycleScrollView2?.delegate = self
//        cycleScrollView2?.onlyDisplayText = true
////        cycleScrollView2?.titleLabelHeight = 50
//        cycleScrollView2?.titleLabelBackgroundColor = UIColor.black
//        cycleScrollView2?.scrollDirection = UICollectionViewScrollDirection.vertical
//        cycleScrollView2?.autoScrollTimeInterval = 5
//        self.scrollView.addSubview(cycleScrollView2!)
        
        let titles = ["感谢您的支持dlk;ajslkjdlksjalkdjsalkjdlksjakldjhioahjdiojqdqlwkjdpoqjpdqjpdqmkcmqpjiowjdklwjdlwjoqjd",
                      "如果代码在使用过程中出现问题djslajldsajldjaljljdlsjldjskjdslkajdlksjalkdjlskjdlsjldjsljdlsjlajdlsjlakjdlsjljdasiojdiojajdio",
                      "您可以发邮件到coderjianfeng@foxmail.com您可以发邮件到coderjianfeng@foxmail.com"
        ]
        titleDemo = LLCycleScrollView.llCycleScrollViewWithTitles(frame: CGRect.init(x: 0, y: 150, width: w, height: 120))
        titleDemo?.customPageControl = .none
        titleDemo?.scrollDirection = .vertical
        titleDemo?.font = UIFont.systemFont(ofSize: 15.0)
        titleDemo?.textColor = UIColor.white
        titleDemo?.backgroundColor = UIColor.black
        titleDemo?.numberOfLines = 0
        titleDemo?.titleLeading = 20
        
        self.scrollView.addSubview(titleDemo!)
        
        
        
        self.homeTable.delegate = self
        self.homeTable.dataSource = self
        //        self.tableView!.backgroundColor = UIColor(red: 0xf0/255, green: 0xf0/255,
        //                                                  blue: 0xf0/255, alpha: 1)
        //去除单元格分隔线
        self.homeTable.separatorStyle = .none
        //创建一个重用的单元格
        self.homeTable.register(UINib(nibName:"HomeTableViewCell", bundle:nil),
                                forCellReuseIdentifier:"myCell")
        
        self.scrollView.addSubview(homeTable)
        
        let isL = UserDefaults.standard.string(forKey: "jwt")
        if isL != nil && isL != ""{

            WebService.shareInstance.jwt = isL
            
        }

//        WebService.shareInstance.jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZU5vIjoiMTU3NTUwMjQwODUiLCJwZXJtaXNzaW9uIjoiMiIsImNyZWF0ZWRBdCI6IjIwMTctMTItMjdUMDI6MjM6MDUuODg0WiIsInVrZXkiOiIxZmQ0OTY2ZS1mMTQwLTQ2OTctYWUxZi1hYjEyNWU4ZTk0OTIiLCJpYXQiOjE1MTYzNTMxMzl9.U2yPztJ46UvM4FnCWTqeqsFgfuYhJRpbD1GX43exQtY"
    
        
    }
    override func viewWillAppear(_ animated: Bool) {
        
        let isLogin = UserDefaults.standard.bool(forKey: "isLogin")
        
        if isLogin == true {
            
            
            WebService.shareInstance.banner{(isSuccess,par)in
                if isSuccess == true{
                    let share = ShareInfo.shareInstance

                    self.imagesURLStrings = NSMutableArray.init(array: share.bannerImgArray)
                    self.cycleScrollView?.imageURLStringsGroup = self.imagesURLStrings as! [Any]
                }else{

                }
            }

            WebService.shareInstance.notice{(isSuccess,par)in

                if isSuccess == true{

                    let share = ShareInfo.shareInstance
                    self.notice = share.noticeArray
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
                        self.titleDemo?.titles = self.notice as! Array<String>
                    }
                }else{

                }

            }
            
            
            
            WebService.shareInstance.activityBrief(pageNo:0 ,pageSize:100) { (isSuccess, par) in

                if isSuccess == true{

                    self.modelArray = NSMutableArray.init(array: ShareInfo.shareInstance.dateArray!)
                    let share = ShareInfo.shareInstance
                    self.scrollView.contentSize = CGSize(width: w, height: (1.0 + CGFloat(share.dateArray.count) ) * self.imageH+30 )

                    self.homeTable.frame = CGRect(x: 0, y: 280, width: w, height: (1.0 + CGFloat(share.dateArray.count) ) * self.imageH)
                    self.homeTable.reloadData()


                }else{
                    let a = ""

                    MBProgressHUD.showToast(to: self.view, withText: a)

                }
            }
            
            self.tabBarController?.tabBar.isHidden = false
            
            
        }else{
            
            let vc = LoginViewController()
            self.present(vc, animated: false) {
               
            }
            
        }
        
    
    
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArray!.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
        -> CGFloat {
            return 250
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let homecell:HomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! HomeTableViewCell
        
        model = modelArray![indexPath.row] as? DataModel
        homecell.activityTitle.text = model?.title
        homecell.activityId.text = model?.activityId
        let picURL = NSURL.init(string: (model?.imgMainUrl)!)
        homecell.avtivityImg.sd_setImage(with: picURL! as URL)
        
        return homecell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let homecell = tableView.cellForRow(at: indexPath) as? HomeTableViewCell
        homecell?.selectionStyle = UITableViewCellSelectionStyle.none
    
        for mod in ShareInfo.shareInstance.dateArray!{
            let mod1 = mod as? DataModel
            if (mod1?.activityId == homecell?.activityId.text) {
                let vc = HomeDetailsViewController()
                vc.acId = mod1?.activityId
                self.navigationController?.pushViewController(vc, animated: true)
                }
        }
    }
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
            let share = ShareInfo.shareInstance
            let mod2 = share.bannerArray[index] as? BannerModel
            if(mod2?.type != 0){
            let vc = HomeDetailsViewController()
                vc.acId = mod2?.keyWord
            self.navigationController?.pushViewController(vc, animated: true)
            }else {
                
            }
        }
    
    }

